<template>
  <div class="add-dialog">
    <!-- 弹窗 -->
    <el-dialog
      :model-value="visible"
      :title="isEdit ? '编辑品牌' : '添加品牌'"
      :close-on-click-modal="false"
      @opened="opendDialog"
      @close="beforeCloseDialog"
      @closed="closeDialog"
    >
      <!-- 表单 -->
      <el-form
        ref="dialogFormRef"
        :model="dialogForm"
        :rules="rules"
        :label-width="120"
        label-position="top"
      >
        <el-form-item label="品牌名称" prop="tmName">
          <el-input
            v-model="dialogForm.tmName"
            placeholder="品牌名称"
            clearable
          />
        </el-form-item>
        <el-form-item label="品牌图片" prop="logoUrl">
          <wu-upload
            v-model:url-prop="dialogForm.logoUrl"
            :upload-url="uploadUrl"
            @onsuccess="onUploadSuccess"
          ></wu-upload>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">Cancel</el-button>
          <el-button type="primary" @click="submitForm(dialogFormRef!)">
            Confirm
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { reactive } from 'vue'
import { uploadUrl } from '@/apis/uploadUrl'
import { FormInstance } from 'element-plus'
import { postAddBaseTrademark, putAddBaseTrademark } from '@/apis/goods'

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  // 编辑form数据
  editForm: {
    type: Object
  }
})
const emits = defineEmits(['update:visible', 'success'])

// 表单数据
const dialogForm = reactive({
  logoUrl: '',
  tmName: ''
})
// 规则
const rules = reactive({
  tmName: [
    { required: true, message: 'Please input Brand LOGO', trigger: 'change' }
  ],
  logoUrl: [
    {
      required: true,
      message: 'Please upload Brand LOGO image',
      trigger: 'change'
    }
  ]
})
// 是否编辑模式
const isEdit = computed(() => props.editForm?.id)
// 弹窗
const opendDialog = () => {
  if (props.editForm?.id) {
    Object.assign(dialogForm, props.editForm)
  }
}
const closeDialog = () => {
  emits('update:visible', false)
  // previewUrl.value = ''
}
// 弹窗关闭之前调用
const beforeCloseDialog = () => {
  console.log('dialogFormRef.value', dialogFormRef.value)
  // 重置表单+清空校验
  dialogFormRef.value?.resetFields()
}
// 提交表单
const dialogFormRef = ref<FormInstance>()
const submitForm = async (formRef: FormInstance) => {
  // 校验
  formRef?.validate(async (valid, fields) => {
    if (valid) {
      const apiFormPromise = isEdit ? putAddBaseTrademark : postAddBaseTrademark
      // 发送请求
      const { code } = await apiFormPromise(dialogForm)
      if (code! === 200) {
        ElMessage({
          message: isEdit ? '编辑成功' : '添加成功',
          type: 'success'
        })
        closeDialog()
        // 通知父组件
        emits('success', props.editForm?.id)
      }
    } else {
      console.warn('fields', fields)
    }
  })
}
// 上传成功回调
const onUploadSuccess = () => {
  dialogFormRef.value?.validateField('logoUrl')
}
</script>

<style lang="scss" scoped>
// upload style
.upload-style .avatar {
  display: block;
  width: 178px;
  height: 178px;
}
</style>

<style>
.upload-style .el-upload {
  position: relative;
  overflow: hidden;
  cursor: pointer;
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  transition: var(--el-transition-duration-fast);
}

.upload-style .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.upload-style-icon {
  width: 130px;
  height: 130px;
  font-size: 28px;
  color: #8c939d;
  text-align: center;
}
</style>
